
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @return {number[]}
*/
var inorderTraversal = function(root) {
  if(!root) return [];
  if(!root.left && !root.right) return [root.val];
  const stack = [], result = [];
  while(stack.length || root) {
    while(root) {
      stack.push(root);
      root = root.left;
    }
    if(stack.length) {
      root = stack.pop();
      result.push(root.val);
      root = root.right;
    }
  }
  return result;
};
